'use strict'

angular.module('mainApp')
.controller('loginCtrl',['$scope', '$rootScope', 'tjzHttp', 'constant', '$state', 'ngDialog', function ($scope,$rootScope,tjzHttp,constant,$state,ngDialog) {
    // 登录页面footer置底start
    $rootScope.isFixedFooter = true;
    $scope.$on('$destroy',function(){
        $rootScope.isFixedFooter = false;
    })
    // 登录页面footer置底end

    // 登录和注册框切换、密码显示切换start
    $scope.loginPage = true;
    $scope.showLpsd = false;
    $scope.showRpsd = false;
    $scope.toggleLogin = function(){
        $scope.loginPage = !$scope.loginPage;
    }
    $scope.toggleLpsd = function(){
        $scope.showLpsd = !$scope.showLpsd;
    }
    $scope.toggleRpsd = function(){
        $scope.showRpsd = !$scope.showRpsd;
    }
    // 登录和注册框切换、密码显示切换end

    // 登录事件start
    $scope.login = function(){
        var data = {
            userName: $scope.loginUsername,
            password: $scope.loginPassword
        }
        var url = constant.url + "/user/login";
        tjzHttp.postRequest(url,data).then(function(res){
            if(res.data.code == 0){
                localStorage.username = $scope.loginUsername;
                localStorage.token = res.data.data.token;
                $rootScope.$broadcast('userLogin');
                $state.go('index');
            }
            else {
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope','ngDialog',function($scope,ngDialog){
                        var message = $scope.ngDialogData.message;
                        $scope.dialogTitle = '登录失败';
                        $scope.dialogContent = message;
                        $scope.yes = function(){
                            $scope.closeThisDialog();
                        }
                    }]
                });
                return;
            }
        })
    }
    // 登录事件end

    // 注册事件start
    $scope.register = function(){
        $scope.register = function(){
            var data = {
                userName: $scope.registerUsername,
                phone: $scope.phone,
                email: $scope.email,
                password: $scope.registerPassword
            }
            var url = constant.url + "/user/register";
            tjzHttp.postRequest(url,data).then(function(res){
                if(res.data.code == 0){
                    localStorage.username = $scope.registerUsername;
                    localStorage.token = res.data.data.token;
                    $rootScope.$broadcast('userLogin');
                    $state.go('index');
                }
                else {
                    var message = res.data.message;
                    ngDialog.open({
                        template: 'template/dialog.html',
                        width: 600,
                        data: {
                            message: message
                        },
                        controller: ['$scope','ngDialog',function($scope,ngDialog){
                            var message = $scope.ngDialogData.message;
                            $scope.dialogTitle = '登录失败';
                            $scope.dialogContent = message;
                            $scope.yes = function () {
                                $scope.closeThisDialog();
                            }
                        }]
                    });
                    return;
                }
            })
        }
    }
    // 注册事件end
}])